package com.example.huadu_server.house.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.huadu_server.house.entity.Favorite;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface FavoriteMapper extends BaseMapper<Favorite> {

    /**
     * 获取收藏量趋势统计
     * @param startDate
     * @param endDate
     * @param granularity
     * @return
     */
    @Select("<script>" +
            "SELECT DATE_FORMAT(time, " +
            "<choose>" +
            "  <when test='granularity == \"day\"'> '%Y-%m-%d' </when>" +
            "  <when test='granularity == \"month\"'> '%Y-%m' </when>" +
            "  <when test='granularity == \"year\"'> '%Y' </when>" +
            "  <otherwise> '%Y-%m-%d' </otherwise>" +
            "</choose>) AS period, " +
            "COUNT(*) AS count " +
            "FROM favority " +
            "WHERE time BETWEEN #{startDate} AND #{endDate} " +
            "GROUP BY period ORDER BY period" +
            "</script>")
    List<Map<String, Object>> selectTrendStats(@Param("startDate") String startDate,
                                               @Param("endDate") String endDate,
                                               @Param("granularity") String granularity);
}
